package com.sskj.fabi.ui.fragment;


import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.SPUtil;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.bean.OrderDetailBean;
import com.sskj.fabi.presenter.OrderRecordFragmentPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Flowable;

@Route(path = RConfig.FABI_FRAGMENT_ORDER_RECORD)
public class OrderRecordFragment extends BaseFragment<OrderRecordFragmentPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @Autowired
    boolean isBuy;
    private HashMap<String, String> statusMap;


    @Override
    protected int getLayoutId() {
        return R.layout.fabi_fragment_order_record;
    }

    @Override
    public OrderRecordFragmentPresenter getPresenter() {
        return new OrderRecordFragmentPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);

        Map<String, String> buyerMap = new HashMap<>();
        Map<String, String> sellerMap = new HashMap<>();
        buyerMap.put("1", "待付款");
        buyerMap.put("2", "等待对方放币");
        buyerMap.put("3", "已完成");
        buyerMap.put("4", "申诉中");
        buyerMap.put("5", "已取消");
        sellerMap.put("1", "等待对方付款");
        sellerMap.put("2", "待收款");
        sellerMap.put("3", "已完成");
        sellerMap.put("4", "申诉中");
        sellerMap.put("5", "已取消");
        statusMap = new HashMap<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 10))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.fabi_f2f))
        );
        SlimAdapter<OrderDetailBean> slimAdapter = SlimAdapter.create().register(R.layout.fabi_recy_item_order_record, new SlimInjector<OrderDetailBean>() {
            @Override
            public void onInject(OrderDetailBean data, IViewInjector injector, List payloads) {
                boolean isBuyer = data.getBuyId().equals(SPUtil.get(SPConfig.ID, ""));

                injector
                        .text(R.id.tvOrderNum, String.format("订单号：%s", data.getDealEntrustNo()))
                        .text(R.id.tvStatus, isBuyer?buyerMap.get(data.getStatus()):sellerMap.get(data.getStatus()))
                        .text(R.id.tvPrice, data.getPrice())
                        .text(R.id.tvNum, data.getTotalNum())
                        .text(R.id.tvMoney, data.getTotalPrice())
                        .text(R.id.tvFee, data.getFee())
                        .text(R.id.tvTime, data.getCreateTime());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<OrderDetailBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<OrderDetailBean>)
                page -> mPresenter.getData(page, 20, isBuy), 20);
        MVCCoolHelper<List<OrderDetailBean>> mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, position, v) -> {
                    ARouter.getInstance().build(RConfig.FABI_ORDER_DETAIL).withString(Constans.ID, slimAdapter.getDataItem(position).getDealEntrustNo()).navigation();
                });
    }
}
